package com.example.chinawenddingexpo.controller;

import com.example.chinawenddingexpo.model.Exhibitor;
import com.example.chinawenddingexpo.service.ExhibitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/exhibitor")
public class ExhibitorController {
    
    @Autowired
    private ExhibitorService exhibitorService;
    
    // 获取所有参展商
    @GetMapping
    public Map<String, Object> getAllExhibitors() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Exhibitor> exhibitors = exhibitorService.getAllActiveExhibitors();
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", exhibitors);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取参展商列表失败");
        }
        return result;
    }
    
    // 按类型获取参展商
    @GetMapping("/type/{type}")
    public Map<String, Object> getExhibitorsByType(@PathVariable Exhibitor.ExhibitorType type) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Exhibitor> exhibitors = exhibitorService.getExhibitorsByType(type);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", exhibitors);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取指定类型参展商失败");
        }
        return result;
    }
    
    // 搜索参展商
    @GetMapping("/search")
    public Map<String, Object> searchExhibitors(@RequestParam String keyword) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Exhibitor> exhibitors = exhibitorService.searchExhibitorsByName(keyword);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", exhibitors);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "搜索参展商失败");
        }
        return result;
    }
    
    // 获取优惠参展商
    @GetMapping("/discount/{type}")
    public Map<String, Object> getDiscountExhibitors(@PathVariable Exhibitor.ExhibitorType type) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Exhibitor> exhibitors = exhibitorService.getDiscountExhibitorsByType(type);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", exhibitors);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取优惠参展商失败");
        }
        return result;
    }
    
    // 获取参展商详情
    @GetMapping("/{id}")
    public Map<String, Object> getExhibitorDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Exhibitor exhibitor = exhibitorService.getExhibitorById(id);
            if (exhibitor != null) {
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", exhibitor);
            } else {
                result.put("code", 404);
                result.put("msg", "参展商不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取参展商详情失败");
        }
        return result;
    }
    
    // 保存参展商
    @PostMapping
    public Map<String, Object> saveExhibitor(@RequestBody Exhibitor exhibitor) {
        Map<String, Object> result = new HashMap<>();
        try {
            Exhibitor savedExhibitor = exhibitorService.saveExhibitor(exhibitor);
            result.put("code", 200);
            result.put("msg", "保存成功");
            result.put("data", savedExhibitor);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "保存参展商失败");
        }
        return result;
    }
}